package com.jn.mvp.presenter;

import android.content.Context;

import com.jn.mvp.contract.HanZiContract;
import com.jn.mvp.entity.HanZi;
import com.jn.mvp.http.GlobalRetrofitClient;
import com.jn.mvp.service.HanZiService;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class HanZiPresenter implements HanZiContract.IHanZiPresenter {

    private HanZiContract.IHanZiView mHanZiView;
    private Context mContext;

    public HanZiPresenter(HanZiContract.IHanZiView hanZiView, Context mContext) {
        this.mHanZiView = hanZiView;
        this.mContext = mContext;
    }

    @Override
    public void getData(String s) {
        HanZiService service = GlobalRetrofitClient.getInstance().create(HanZiService.class);
        service.getData(s)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<HanZi>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull HanZi hanZi) {
                        mHanZiView.showData(hanZi);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        mHanZiView.onHttpFail(e.getMessage());
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }
}
